<extend name="public/form"/>
<block name="css"></block>
<block name="content">
    <div class="layui-card-header "></div>
    <div class="layui-card-body layui-tab"><!--选项卡父容器必须包含layui-tab class-->
        <ul class="layui-tab-title">
            <li class="layui-this">修改基本信息</li>
            <li>授权角色</li>
            <li>重置密码</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <form id="form1" class="layui-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-block">
                            <input type="hidden" name="id" value="{{$user.id}}">
                            <input type="text" name="username" lay-verify="required" lay-reqtext="请输入用户名"
                                   placeholder="请输入用户名" autocomplete="off" class="layui-input "
                                   value="{{$user.username}}" disabled>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" lay-reqtext="请输入姓名"
                                   placeholder="请输入姓名" autocomplete="off" class="layui-input "
                                   value="{{$user.username}}">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-block">
                            <input type="text" name="mobile" lay-verify="phone" lay-reqtext="请输入手机号"
                                   placeholder="请输入手机号" autocomplete="off" class="layui-input "
                                   value="{{$user.mobile}}">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">邮箱</label>
                        <div class="layui-input-block">
                            <input type="text" name="email" lay-verify="email" lay-reqtext="请输入邮箱"
                                   placeholder="请输入邮箱" autocomplete="off" class="layui-input "
                                   value="{{$user.email}}">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="0" title="男"
                            <if condition="$user.sex==0">checked</if>
                            />
                            <input type="radio" name="sex" value="1" title="女"
                            <if condition="$user.sex==1">checked</if>
                            />
                            <input type="radio" name="sex" value="2" title="保密" disabled=""
                            <if condition="$user.sex==2">checked</if>
                            />
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <input type="radio" name="status" title="启用" value="1" checked/>
                            <input type="radio" name="status" title="禁用" value="0"/>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-tab-item">
                <form id="form3" class="layui-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-block">
                            <input type="text" autocomplete="off" class="layui-input " value="{{$user.username}}"
                                   disabled>
                        </div>
                    </div>
                    <input type="hidden" name="id" value="{{$user.id}}">
                    <div class="layui-form-item" pane="">
                        <label class="layui-form-label">授权角色</label>
                        <div class="layui-input-block">
                            <volist name="groups" id="vo">
                                <input type="checkbox" name="groups[]" {{$vo.checked}} value="{{$vo.id}}"
                                       lay-skin="primary" title="{{$vo.title}}">
                            </volist>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="auth">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-tab-item">
                <form id="form2" class="layui-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input type="hidden" name="id" value="{{$user.id}}">
                            <input type="password" name="password" lay-verify="required" lay-reqtext="请输入密码"
                                   placeholder="请输入密码" autocomplete="off" class="layui-input ">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">重复密码</label>
                        <div class="layui-input-block">
                            <input type="password" name="rpassword" lay-verify="required|rpassword" lay-reqtext="请再次输入密码"
                                   placeholder="请再次输入密码" autocomplete="off" class="layui-input ">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="edit_pwd">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</block>
<block name="js">
    <script>
        layui.use(['jquery', 'form', 'layer', 'tree', 'util', 'element',], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                tree = layui.tree,
                util = layui.util,
                element = layui.element;
            form.render();
            //监听修改基本信息提交
            form.on('submit(add)', function (data) {
                let send_json = data.field;
                let url='/admin/user/edit/id/'+send_json.id;
                delete send_json.id;
                $.ajax({
                    url: url,
                    method: 'post',
                    data: send_json,
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == 0) {
                            layer.alert("编辑成功", {
                                icon: 6
                            }, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload();
                                //关闭当前frame
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });
                return false;
            });
            //监听修改授权管理
            form.on('submit(auth)', function (data) {
                let send_json = data.field;
                //   console.log(send_json);  return false;
                url='/admin/user/authGroup/id/'+send_json.id;
                delete send_json.id;
                $.ajax({
                    url: url,
                    method: 'post',
                    data: send_json,
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == 0) {
                            layer.alert("授权成功成功", {
                                icon: 6
                            }, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });
                return false;
            });
            //监听修改密码提交
            form.on('submit(edit_pwd)', function (data) {
                let send_json = data.field;
                let url='/admin/user/password/id/'+send_json.id;
                delete send_json.id;
                $.ajax({
                    url: url,
                    method: 'post',
                    data: send_json,
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == 0) {
                            layer.alert("编辑成功", {
                                icon: 6
                            }, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });
                return false;
            });
            //自定义验证规则
            form.verify({
                rpassword: function (value, item) { //value：表单的值、item：表单的DOM对象
                    var pwd = $('input[name="password"]').val();
                    if (pwd != value) {
                        return '两次密码输入不一致！';
                    }
                }

                //我们既支持上述函数式的方式，也支持下述数组的形式
                //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
                , pass: [
                    /^[\S]{6,12}$/
                    , '密码必须6到12位，且不能出现空格'
                ]
            });
            //遍历select option
            $(document).ready(function () {
                $("#pid-select option").each(function (text) {
                    var level = $(this).attr('data-level');
                    var text = $(this).text();
                    if (level > 1) {
                        text = "├" + text;
                        for (var i = 1; i <= level; i++) {
                            text = "　　" + text;　//js中连续显示多个空格，需要使用全角的空格
                            //console.log(i+"text:"+text);
                        }
                    }
                    $(this).text(text);
                });
                form.render('select'); //刷新select选择框渲染
            });
        });
    </script>
</block>